<template>
    <div class="editor">
        <Modal v-model="modalShow"
               title="新增"
               width="500">
            <Form ref="formValidate"
                  :model="form"
                  :label-width="90">
                <FormItem label="姓名"
                          prop="name">
                    <Input v-model="form.name"
                           placeholder="姓名"></Input>
                </FormItem>
                <FormItem label="手机"
                          prop="phone">
                    <Input v-model="form.phone"
                           placeholder="手机号"></Input>
                </FormItem>
                <FormItem label="邀请码">
                    <Input v-model="form.invite_code"
                           placeholder="邀请码"></Input>
                </FormItem>
                <FormItem label="公司">
                    <Input v-model="form.coname"
                           placeholder="公司"></Input>
                </FormItem>
                <FormItem label="部门">
                    <Input v-model="form.divname"
                           placeholder="部门"></Input>
                </FormItem>
            </Form>

            <div slot="footer">
                <Button type="text"
                        @click="modalShow = false">取消</Button>
                <Button type="primary"
                        :loading="loading"
                        @click="comfirm">确定</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
export default {
    props: {
        type: String // new, update
    },
    data() {
        return {
            loading: false,
            modalShow: false,
            top: false,
            form: {
                project_id: "",
                name: "",
                phone: "",
                invite_code: "",
                coname: "",
                divname: "",
            }
        };
    },
    methods: {
        show() {
            // 读取全局变量中的当前对象
            var poid = localStorage.pid
            this.form = {
                project_id: poid,
                name: "",
                phone: "",
                invite_code: "",
                coname: "",
                divname: "",
            }
            this.modalShow = true;
        },
        comfirm() {
            this.loading = true;
            if (this.type === "new") {
                $.post('https://evp.51job.com/51job/api/1.0/index.php/minirs/user/adduser',
                    this.form,
                    res => {
                        if(res.code === 1) {
                            // 成功
                            this.loading = false;
                            this.$Message.success('添加成功')
                            this.modalShow = false;
                            // 刷新列表
                            this.$emit('success')
                        } else {
                            this.loading = false;
                            this.$Message.error('添加失败：' + res.msg);
                            this.modalShow = false;
                        }
                })
            }
        }
    },
    components: {
    }
};
</script>

<style></style>
